<?php

namespace App\Module\Sub;

use App\depository\ApiLogDep;
use App\depository\SubApiLogDep;
use App\Enum\ApiLogEnum;
use App\Enum\CommonEnum;
use App\Module\BaseModule;
use App\depository\UserDep;

class SubApiLogModule extends BaseModule
{

    public function del($request)
    {
        $param = $request->input();

        $dep = new SubApiLogDep();

        $dep->del($param['id']);
        return self::response();
    }

    public function list($request)
    {
        $param = $request->input();
        $teamId = $request->headers->get('TeamId');
        $user = $request->user();

        $dep = new SubApiLogDep();
        $userDep = new UserDep();
        $param['user_id'] = $user->id;

        $resList = $dep->list($teamId,$param);

        $data['list'] = $resList->map(function($item) use($userDep,$user) {
            $resUser = $userDep->firstById($item->user_id);
            return [
                'id' => $item->id,
                'ip' => $item->ip,
                'title' => ApiLogEnum::$statusArr[$item->path] ?? CommonEnum::DEFAULT_NULL,
                'user_id' => $item->user_id,
                'user_show' => $resUser ? $resUser->name : CommonEnum::DEFAULT_NULL,
                'path' => $item->path,
                'method' => $item->method,
                'input' => $user->is_admin=== CommonEnum::YES ? $item->input :"无权查看",
                'created_at' => $item->created_at->toDateTimeString()
            ];
        });

        $data['page'] = [
            'per_page' => $param['per_page'],
            'current_page' => $param['current_page'],
            'total_page' => $resList->lastPage(),
            'total' => $resList->total(),
        ];

        return self::response($data);
    }


}

